package com.os.opencv.java.chapter10;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class HoughCircle {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //读取图像并在屏幕上显示
        Mat src = Imgcodecs.imread("pics/goChild.png");
        HighGui.imshow("src", src);
        HighGui.waitKey(0);

        //预处理
        Mat gray = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.GaussianBlur(gray, gray, new Size(9,9), 2);

        //霍夫检测
        Mat circles = new Mat();
        Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT,1,10,100,30,5,30);
        //将检测出的圆花圈
        for(int n=0; n<circles.cols(); n++){
            double[] c = circles.get(0, n);
            Point center = new Point(Math.round(c[0]), Math.round(c[1]));  //圆心
            int radius = (int) Math.round(c[2]);  //半径
            Imgproc.circle(src, center, radius, new Scalar(0,0,255), 5);
        }
        //在屏幕上显示检测结果
        HighGui.imshow("circles", src);
        HighGui.waitKey(0);
        System.exit(0);
    }
}
